package com.zjy.business.system.dao;

import com.zjy.business.system.domain.entity.SysRole;
import com.zjy.business.system.domain.entity.SysUserRole;
import com.zjy.business.system.domain.manager.query.ManagerListPageSysRoleQuery;
import com.zjy.business.system.domain.manager.vo.ManagerListPageSysRoleVo;
import com.zjy.business.system.mapper.SysRoleMapper;
import com.zjy.common.constant.StatusEnums;
import com.zjy.common.utils.StreamUtils;
import com.zjy.config.RedisServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * @author yxb
 * create_time: 2024-10-22 14:44
 * description: 
 */
@Service
public class SysRoleDao extends RedisServiceImpl<SysRoleMapper, SysRole> {

    @Resource
    @Lazy
    private SysUserRoleDao sysUserRoleDao;

    public List<ManagerListPageSysRoleVo> listPageSysRole(ManagerListPageSysRoleQuery query) {
        return this.baseMapper.listPageSysRole(query);
    }

    /**
     * 根据用户id获取关联角色key
     * @param userId 用户id
     * @return
     */
    public List<String> getRoleKeyByUserId(Long userId) {
        List<Long> roleIds = StreamUtils.toList(sysUserRoleDao.findCacheList(SysUserRole::getUserId, userId), SysUserRole::getRoleId);
        return StreamUtils.toList(findCacheOneByMoreParam(SysRole::getId, roleIds).values(), item -> {
            if (Objects.equals(item.getStatus(), StatusEnums.PUBLIC_STATUS_NORMAL.getStatus())) {
                return item.getRoleKey();
            }
            return null;
        });
    }
}
